package JeuInterfaces;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;


import GameBoard.GameBoardFactory;
import GameElements.Tile;
import MoteurJeu.MoteurJeu;
import Player.Player;

public class JeuInterfaceGraphique extends JeuInterfaceGraphiqueAbstract
{
	private MoteurJeu moteurJeu;
	private JButton piocherButton;
	public static final int PIOCHER_EVENT=0, CLICK_GAME_PANEL=1, TOURNER_EVENT=2, TERMINER_EVENT=3;
	private static int currentEvent;
	private static Point clickedTile = new Point(0, 0);
	private static int tilePart = 0; 
	
	public JeuInterfaceGraphique(Vector<Player> joueursH,String versionJeu)
	{
		super(joueursH,versionJeu);
		
		GameBoardFactory gameBoardFactory = GameBoardFactory.getInstance();
		gameBoard = gameBoardFactory.createGameBoard(versionJeu);
		
		moteurJeu = new MoteurJeu(gameBoard, joueursH, this);
		tuileSize = gameBoard.getStock().get(0).getImage().getIconWidth();
		piocherButton = new JButton(new ImageIcon("img/tiles/dosCarte.gif"));
		piocherButton.addActionListener(new PiocherActionListner());
	}	
	
	public void update()
	{
		updatePioche();
		miseAJourGrille();
		window.validate();
	}

	private void updatePioche()
	{
		Player currentPlayer = moteurJeu.getCurrentPlayer();

		if(currentPlayer != null)
		{
			tuileCourante = currentPlayer.getTuilePioche();
			if(tuileCourante != null)
				piocherButton.setIcon(new ImageIcon(tuileCourante.getImage().getImage()));
			else
				piocherButton.setIcon(new ImageIcon("img/tiles/dosCarte.gif"));
		}
		else
			piocherButton.setIcon(new ImageIcon("img/tiles/dosCarte.gif"));
		
		TuilesOptionsPanel.add(piocherButton);		
		TuilesOptionsPanel.setBackground(moteurJeu.getCurrentPlayer().getColor());
	}
	
	
	protected void mouseClickedGamePanelAction(MouseEvent e)
	{
		clickedTile.x = e.getX() / tuileSize;
		clickedTile.y = e.getY() / tuileSize;
		tilePart = positionTuile(e.getX()-(clickedTile.x*tuileSize), e.getY()-(clickedTile.y*tuileSize), tuileSize, tuileSize);
		currentEvent = CLICK_GAME_PANEL;
		moteurJeu.run();
	}

	protected void buttonTournerAction(ActionEvent e)
	{
		currentEvent = TOURNER_EVENT;
		moteurJeu.run();
	}
	
	protected void buttonTermineTourAction(ActionEvent e)
	{
		currentEvent = TERMINER_EVENT;
		moteurJeu.run();
	}
	
	protected void buttonPiocherAction(ActionEvent e)
	{
		currentEvent = PIOCHER_EVENT;
		moteurJeu.run();
	}
	
	
	private class PiocherActionListner implements ActionListener 
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			buttonPiocherAction(e);
		}
	}

	protected void mouseClickedTuileAction(MouseEvent e, Tile t)
	{

	}	
	
	//getter
	public static int getCurrentEvent()
	{
		return currentEvent;
	}
	public static Point getClickedTile()
	{
		return clickedTile;
	}
	public static int getTilePart()
	{
		return tilePart;
	}
}
